﻿using MySql.Data.MySqlClient;
using System;
using System.Data;

namespace HolterDatabase.Model
{
    public class PatientSet : DatabaseSet
    {
        public override MySqlDataAdapter GetAdapter(MySqlConnection connection)
        {
            var adapter = new MySqlDataAdapter("SELECT * FROM patient", connection);

            // Update command
            string updateString = "UPDATE patient SET FirstName=?FirstName, LastName=?LastName, BirthDate=?BirthDate, Adress=?Adress, Phone=?Phone WHERE Id=?Id ";
            MySqlCommand updateCommand = new MySqlCommand(updateString, connection);
            updateCommand.Parameters.Add("?FirstName", MySqlDbType.VarChar, 256, "FirstName");
            updateCommand.Parameters.Add("?LastName", MySqlDbType.VarChar, 256, "LastName");
            updateCommand.Parameters.Add("?BirthDate", MySqlDbType.Date, 10, "BirthDate");
            updateCommand.Parameters.Add("?Adress", MySqlDbType.VarChar, 256, "Adress");
            updateCommand.Parameters.Add("?Phone", MySqlDbType.VarChar, 20, "Phone");
            MySqlParameter parameter = updateCommand.Parameters.Add("?Id", MySqlDbType.Int32, 10, "Id");
            parameter.SourceVersion = DataRowVersion.Original;
            adapter.UpdateCommand = updateCommand;

            // Insert Command
            string insertString = "INSERT INTO patient (FirstName, LastName, BirthDate, Adress, Phone) " +
                                  "VALUES (?FirstName, ?LastName, ?BirthDate, ?Adress, ?Phone)";
            MySqlCommand insertCommand = new MySqlCommand(insertString, connection);
            insertCommand.Parameters.Add("?FirstName", MySqlDbType.VarChar, 256, "FirstName");
            insertCommand.Parameters.Add("?LastName", MySqlDbType.VarChar, 256, "LastName");
            insertCommand.Parameters.Add("?BirthDate", MySqlDbType.Date, 10, "BirthDate");
            insertCommand.Parameters.Add("?Adress", MySqlDbType.VarChar, 256, "Adress");
            insertCommand.Parameters.Add("?Phone", MySqlDbType.VarChar, 20, "Phone");
            adapter.InsertCommand = insertCommand;

            // Delete Command
            MySqlCommand deleteCommand = new MySqlCommand("DELETE FROM patient WHERE patient.Id=?Id", connection);
            MySqlParameter delParameter = deleteCommand.Parameters.Add("?Id", MySqlDbType.Int32, 10, "Id");
            delParameter.SourceVersion = DataRowVersion.Original;
            adapter.DeleteCommand = deleteCommand;

            return adapter;
        }
    }
}
